//
//  WCG_N_ProviderDetailHeaderView.m
//  Project_B
//
//  Created by apple on 15/11/26.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_N_ProviderDetailHeaderView.h" // 第二页头部视图

#import "GXProviderDetailModel.h"          // 第二页Model
#import "NSString+GXSizeWithText.h"        // 自适应尺寸
#import "UIImageView+GXCircleImageView.h"  // 圆形图片裁切方法
#import "UIImageView+WebCache.h"           // 网络图片

//左边界
#define kProviderCellLeftBorder 20
//上边界
#define kProviderCellTopBorder 25

//图片边长

#define kProviderCellImageViewSize 55

//标题与图片的距离
#define kProviderCellLabelLeftBorder 20
//简介与标题的距离  ???
#define kProviderCellDescLabelTopBorder 5

//标题字号
#define kProviderTitleLabelFontSize 20
//简介字号
#define kProviderDescLabelFontSize 14

//名字高度 ???
#define kProviderNameLabelHeight 30


@interface WCG_N_ProviderDetailHeaderView ()

// 图片
@property (nonatomic, retain) UIImageView *providerImageView;
// 名字
@property (nonatomic, retain) UILabel *providerNameLabel;

@end

@implementation WCG_N_ProviderDetailHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];        // 头部视图的建立(通过子视图的方法)
        [self initWithSubviewsFrame]; // 头部视图的尺寸方法调用
    }
    return self;
}


- (void)createSubviews {
    // 图片
    self.providerImageView = [[UIImageView alloc] init];
    [self addSubview:self.providerImageView];
    
    // 名字
    self.providerNameLabel = [[UILabel alloc] init];
    [self.providerNameLabel setFont:[UIFont systemFontOfSize:kProviderTitleLabelFontSize]];
    self.providerDescLabel.textColor = [UIColor blackColor];
    [self addSubview:self.providerNameLabel];
    
    // 简介
    self.providerDescLabel = [[UILabel alloc] init];
    [self.providerDescLabel setFont:[UIFont systemFontOfSize:kProviderDescLabelFontSize]];
    self.providerDescLabel.textColor = [UIColor grayColor];
    [self addSubview:self.providerDescLabel];
}

- (void)initWithSubviewsFrame {
    
    // 图片(调用了圆形图片的裁剪方法)
    [self.providerImageView setCircleImageViewFrame:CGRectMake(kProviderCellLeftBorder, kProviderCellTopBorder, kProviderCellImageViewSize, kProviderCellImageViewSize)];

    //名字frame的计算 过程
    CGFloat nameLabelX = CGRectGetMaxX(self.providerImageView.frame) + kProviderCellLabelLeftBorder;
    CGFloat nameLabelW = self.frame.size.width - nameLabelX - 3.5/2.0 * kProviderCellLeftBorder;
    [self.providerNameLabel setFrame:CGRectMake(nameLabelX, kProviderCellTopBorder, nameLabelW, kProviderNameLabelHeight)];
    
    //简介 frame的计算 过程
    CGRect descLabelSize = [self.providerDescLabel.text SizeWithTextFontSize: kProviderDescLabelFontSize textWidth:nameLabelW];
    descLabelSize.size.height = descLabelSize.size.height > 50 ? 50 : descLabelSize.size.height;
    CGFloat descLabelY = CGRectGetMaxY(self.providerNameLabel.frame) + kProviderCellDescLabelTopBorder;
    [self.providerDescLabel setFrame:CGRectMake(nameLabelX, descLabelY, nameLabelW, descLabelSize.size.height)];
}

// 赋值方法
- (void)setProviderDetailModel:(GXProviderDetailModel *)providerDetailModel {
    if (_providerDetailModel != providerDetailModel) {
        _providerDetailModel = providerDetailModel;
    }
    [self.providerImageView sd_setImageWithURL:_providerDetailModel.user[@"avatar"]];
    
    self.providerNameLabel.text = _providerDetailModel.user[@"screen_name"];
    self.providerDescLabel.text = _providerDetailModel.user[@"description"];
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
